package com.flame.shapeville.util;

import org.yaml.snakeyaml.DumperOptions;
import org.yaml.snakeyaml.introspector.Property;
import org.yaml.snakeyaml.nodes.MappingNode;
import org.yaml.snakeyaml.representer.Representer;

import java.util.Map;
import java.util.Set;

/**
 * Custom Representer that respects the `@YamlIgnore` annotation during YAML serialization.
 *
 * @author Flame Dev Team
 * @version 1.0
 * @since 2025-05-08
 */
public class JfRepresenter extends Representer {
    /**
     * Constructs a new JfRepresenter with given options.
     *
     * @param options YAML dumper options
     */
    public JfRepresenter(DumperOptions options) {
        super(options);
    }

    /**
     * Represents a Java bean while filtering out fields annotated with `@YamlIgnore`.
     *
     * @param properties Properties of the bean
     * @param javaBean Bean instance to represent
     * @return MappingNode representation of the bean
     */
    @Override
    protected MappingNode representJavaBean(Set<Property> properties, Object javaBean) {
        properties.removeIf(property ->
        {
            try {
                return javaBean.getClass().getDeclaredField(property.getName()).isAnnotationPresent(YamlIgnore.class);
            } catch (NoSuchFieldException e) {
                throw new RuntimeException(e);
            }
        });
        return super.representJavaBean(properties, javaBean);
    }
}
